package com.qding.park.manager.service.core.facade.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.common.core.exceptions.business.payment.PaymentException;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.common.utils.ListUtil;
import com.qding.park.common.utils.OrderNumUtil;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.facade.core.service.DemoFacade;
import com.qding.park.manager.payment.PaymentInterfaceServiec;
import com.qding.park.manager.pojo.TbCarlock;
import com.qding.park.manager.pojo.TbHistoryRecord;
import com.qding.park.manager.pojo.TbHistoryRecord.RecordTypeEnum;
import com.qding.park.manager.pojo.TbInvitecard;
import com.qding.park.manager.pojo.TbParkspace;
import com.qding.park.manager.pojo.enums.CarLockStatusEnum;
import com.qding.park.manager.pojo.enums.OrderServiceType;
import com.qding.park.manager.pojo.enums.OrderSourceEnum;
import com.qding.park.manager.pojo.payment.AbstractResult;
import com.qding.park.manager.pojo.payment.OrderParameter;
import com.qding.park.manager.pojo.payment.ResultCreateorder;
import com.qding.park.manager.service.core.biz.DemoBiz;
import com.qding.park.manager.service.core.biz.DemoBiz.DemoUserMonth;
import com.qding.park.manager.service.core.config.ParkTempStopNumReserve;

/**
 * @author Administrator 演示服务实现
 */
@Service("demoFacade")
public class DemoFacadeImpl implements DemoFacade {

	

	@Autowired
	private DemoBiz demoBiz;

	@Autowired
	private PaymentInterfaceServiec paymentInterfaceServiec;

	@Override
	public ParkDataResult getParkings(String userId, String phone, String projectId) throws BaseException {
		List<DBObject> datas = demoBiz.demoData_getParkings();

		List list = new ArrayList();
		for (DBObject parkspace : datas) {
			Map<String, Object> map = new HashMap<String, Object>();
			int parkId = (int) parkspace.get("parkId");
			String parkName = (String) parkspace.get("parkName");
			int tempStopNum = (int) parkspace.get("tempStopNum");
			int bookTime = (int) parkspace.get("bookTime");
			int parkStatus = (int) parkspace.get("parkStatus");
			List parkedCarList = (List) parkspace.get("parkedCarList");

			map.put("parkId", parkId);
			map.put("parkName", parkName);
			map.put("tempStopNum", tempStopNum - ParkTempStopNumReserve.getPark_tempStopNum_reserve() < 0 ? 0
					: tempStopNum - ParkTempStopNumReserve.getPark_tempStopNum_reserve());
			map.put("bookTime", bookTime * 60);
			map.put("parkStatus", parkStatus);
			map.put("parkedCarList", new ArrayList());

			demoBiz.findMyCars(userId);
			long expireTime = 0L;
			for (int i = 0; i < parkedCarList.size(); i++) {
				BasicDBObject car = (BasicDBObject) parkedCarList.get(i);
				// 此处要算出最近到到期时间
				Map<String, Object> map1 = new HashMap<String, Object>();
				map1.put("carNum", car.getString("carNum"));

				((List) map.get("parkedCarList")).add(map1);
				if (car.getDate("cardExpiration").getTime() > expireTime) {
					expireTime = car.getDate("cardExpiration").getTime();
				}
			}
			map.put("cardExpiration", expireTime == 0 ? 0 : expireTime / 1000L);
			list.add(map);
		}
		ParkDataResult data = ParkDataResult.createListDate();
		data.putDataOnList(list);
		return data;
	}

	@Override
	public ParkDataResult queryMyCarsByBjcode(String bjUserId) {
//		List<DBObject> datas = demoBiz.demoData();
		
		List<DBObject> datas = demoBiz.findMyCars(bjUserId);
		if (ListUtil.isEmpty(datas)) {
			return ParkDataResult.createEmpty();
		}
		ParkDataResult parkDataResult = ParkDataResult.createListDate();
		List list = new ArrayList();
		Map<String, Map<String, Object>> result = new HashMap<String, Map<String, Object>>();
		for (DBObject car : datas) {
			String carNum = (String) car.get("carNum");
			int isMonthCard = (int) car.get("isMonthCard");
			String parkName = (String) car.get("parkName");
			int parkId = (int) car.get("parkId");
			Date cardExpiration = (Date) car.get("cardExpiration");

			if (result.containsKey(carNum)) {
				Map<String, Object> carinfo = result.get(carNum);
				List ll = (List) carinfo.get("parkList");
				Map<String, Object> tempmap_01 = new HashMap<String, Object>();
				tempmap_01.put("cardExpiration", cardExpiration.getTime() / 1000L);
				tempmap_01.put("parkId", parkId);
				tempmap_01.put("parkName", parkName);
				ll.add(tempmap_01);
			} else {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("carNum", carNum);
				map.put("isMonthCard", isMonthCard);
				List parklist = new ArrayList();
				map.put("parkList", parklist);

				Map<String, Object> tempmap = new HashMap<String, Object>();
				tempmap.put("cardExpiration", cardExpiration.getTime() / 1000L);
				tempmap.put("parkId", parkId);
				tempmap.put("parkName", parkName);

				parklist.add(tempmap);

				result.put(carNum, map);
			}

		}

		for (Map<String, Object> val : result.values()) {
			list.add(val);
		}
		parkDataResult.putDataOnList(list);

		return parkDataResult;
	}

	@Override
	public ParkDataResult queryCarInfo(String bjuserid, String carNum) throws BaseException {

		List<DBObject> datas = demoBiz.findCarInfo(bjuserid, carNum);
//		DBObject  car=demoBiz.findCarInfo(bjuserid, carNum);
		ParkDataResult parkDataResult = ParkDataResult.createListDate();
		if (ListUtil.isEmpty(datas)) {
			return ParkDataResult.createEmpty();
		}

		List list = new ArrayList();
		for (DBObject car : datas) {
			Map<String, Object> map = new HashMap<String, Object>();
			int isMonthCard = (int) car.get("isMonthCard");
			String parkName = (String) car.get("parkName");
			int parkId = (int) car.get("parkId");
			Date cardExpiration = (Date) car.get("cardExpiration");
			map.put("carNum", carNum);
			map.put("isMonthCard", isMonthCard);
			map.put("parkName", parkName);
			map.put("parkId", parkId);
			map.put("cardExpiration", cardExpiration.getTime() / 1000L);
			list.add(map);
		}
		parkDataResult.putDataOnList(list);
		return parkDataResult;
	}

	@Override
	public ParkDataResult createReserveOrder(String bjuserId, int parkId, String carNum, Date reserveStartTime,
			String project) throws BaseException {

		OrderParameter orderparameter = new OrderParameter();
		int orderType = OrderParameter.OrderTypeEnum.RESERVER_TYPE.getType();
		int orderSource = OrderSourceEnum.APP.getType();
		String szCarpassId = OrderNumUtil.getOrderNum(1, OrderServiceType.RESERVER.getTag());
		szCarpassId = szCarpassId + OrderServiceType.DEMO_ORDER.getTag();
		orderparameter.setOrderType(orderType);
		orderparameter.setTotalPrice(1);
		orderparameter.setOrderSource(orderSource);
		orderparameter.setSzCarpassId(szCarpassId);
		orderparameter.setMemberId(bjuserId);
		orderparameter.setProjectId(project);

		ResultCreateorder resultCreateorder = paymentInterfaceServiec.createOrder(orderparameter);
		if (resultCreateorder.getCode() != AbstractResult.ResultCodeEnum.SUCESS.getCode()) {
			throw new PaymentException(PaymentException.TypeEnum.EXCEPTION_PAYMENT_FAIL.getId(), null,
					ExceptionMessage.createExceptionMessage(PaymentException.TypeEnum.EXCEPTION_PAYMENT_FAIL.getDesc(),
							"创建订单失败:payPlatform:" + resultCreateorder.getMessage(),
							" orderType=" + orderType + ",totalPrice=" + 0 + ",orderSource" + orderSource
									+ ",szCarpassId=" + szCarpassId + ",memberId=" + bjuserId + ",orderId"
									+ szCarpassId));
		}

		demoBiz.demoData_createReserveOrder(parkId, carNum, bjuserId, szCarpassId);
		ParkDataResult parkDataResult = new ParkDataResult();
		parkDataResult.putDataOnObj("szOrderId", szCarpassId);
		parkDataResult.putDataOnObj("bjOrderId", resultCreateorder.getOrderId());
		return parkDataResult;
	}

	@Override
	public ParkDataResult getParkBookRules(int parkId) throws BaseException {
		ParkDataResult parkDataResult = new ParkDataResult();
		List<DBObject> list = demoBiz.demoData_record_findAll(demoBiz.PARK_RECORD_TYPE, parkId);
		if (ListUtil.isEmpty(list)) {
			// 预定金额
			parkDataResult.putDataOnObj("depositExplain", 0);
		} else {
			int depositExplain = (int) list.get(0).get("depositExplain");
			parkDataResult.putDataOnObj("depositExplain", depositExplain);
		}

		// 预定说明文本
		parkDataResult.putDataOnObj("reserverContect", "1：预定停车位后在规定时间内进场不用担心没有车位。");
		return parkDataResult;
	}

	@Override
	public ParkDataResult createTempPayment_Temp(int parkId, String carNum, String bjuserId) throws BaseException {
		DBObject temporder = demoBiz.demoData_record_findone(carNum, RecordTypeEnum.TEMP_PAY.getCode());
//		String orderId = (String) temporder.get("orderId");
		String parkName = (String) temporder.get("parkName");
		int parkFee = (int) temporder.get("deposit");
		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		
		/****保存用户配置****/
		String szCarpassId = OrderNumUtil.getOrderNum(1, OrderServiceType.TEMP_PAY.getTag());
		szCarpassId = szCarpassId + OrderServiceType.DEMO_ORDER.getTag();
		DemoBiz.DemoUserTemp temp=new DemoBiz.DemoUserTemp();
		temp.setOrderId(szCarpassId);
		temp.setParkId(parkId);
		temp.setCarNum(carNum);
		temp.setParkName(parkName);
		demoBiz.demoData_createTempOrder(bjuserId,temp);
		/****保存用户会话****/
		
		parkDataResult.putDataOnObj("orderId", szCarpassId);
		parkDataResult.putDataOnObj("parkId", parkId);
		parkDataResult.putDataOnObj("parkName", parkName);
		parkDataResult.putDataOnObj("startTime", DateUtils.addHour(new Date(), -1).getTime() / 1000L);
		parkDataResult.putDataOnObj("endTime", new Date().getTime() / 1000);
		parkDataResult.putDataOnObj("parkFee", parkFee);
		return parkDataResult;
	}

	@Override
	public ParkDataResult createTemPayOrder_Temp(String orderId, String project) {

		OrderParameter orderparameter = new OrderParameter();
		int orderType = OrderParameter.OrderTypeEnum.TEMP_PAY_TYPE.getType();
		int orderSource = OrderSourceEnum.APP.getType();
//		DBObject record = demoBiz.demoData_record_findone(orderId);
//		int totalPrice = (int) record.get("deposit");
		 DemoBiz.DemoUserTemp temp=demoBiz.findTemp(orderId);
		String memberId = temp.getUsreId();
		orderparameter.setOrderType(orderType);
		orderparameter.setTotalPrice(1);
		orderparameter.setOrderSource(orderSource);
		orderparameter.setSzCarpassId(orderId);
		orderparameter.setMemberId(memberId);
		orderparameter.setProjectId(project);

		ResultCreateorder resultCreateorder = paymentInterfaceServiec.createOrder(orderparameter);
		if (resultCreateorder.getCode() != AbstractResult.ResultCodeEnum.SUCESS.getCode()) {
			throw new PaymentException(PaymentException.TypeEnum.EXCEPTION_PAYMENT_FAIL.getId(), null,
					ExceptionMessage.createExceptionMessage(PaymentException.TypeEnum.EXCEPTION_PAYMENT_FAIL.getDesc(),
							"创建订单失败:payPlatform:" + resultCreateorder.getMessage(),
							" orderType=" + orderType + ",totalPrice=" + 1 + ",orderSource" + orderSource
									+ ",szCarpassId=" + orderId + ",memberId=" + memberId + ",orderId" + orderId));
		}
		ParkDataResult dataResult = ParkDataResult.createObjDate();
		dataResult.putDataOnObj("szOrderId", orderId);
		dataResult.putDataOnObj("bjOrderId", resultCreateorder.getOrderId());
		return dataResult;
	}

	@Override
	public ParkDataResult createMonthPayOrder_Mon(String orderId, String project) {
		OrderParameter orderparameter = new OrderParameter();
		int orderType = OrderParameter.OrderTypeEnum.MONTH_PAY_TYPE.getType();
		int orderSource = OrderSourceEnum.APP.getType();
//		DBObject record = demoBiz.demoData_record_findone(orderId);
	  DemoBiz.DemoUserMonth month=demoBiz.findMonth(orderId);
		int totalPrice = 1;
		String memberId = month.getUserId();
		orderparameter.setOrderType(orderType);
		orderparameter.setTotalPrice(totalPrice);
		orderparameter.setOrderSource(orderSource);
		orderparameter.setSzCarpassId(orderId);
		orderparameter.setMemberId(memberId);
		orderparameter.setProjectId(project);

		ResultCreateorder resultCreateorder = paymentInterfaceServiec.createOrder(orderparameter);
		if (resultCreateorder.getCode() != AbstractResult.ResultCodeEnum.SUCESS.getCode()) {
			throw new PaymentException(PaymentException.TypeEnum.EXCEPTION_PAYMENT_FAIL.getId(), null,
					ExceptionMessage.createExceptionMessage(PaymentException.TypeEnum.EXCEPTION_PAYMENT_FAIL.getDesc(),
							"创建订单失败:payPlatform:" + resultCreateorder.getMessage(),
							" orderType=" + orderType + ",totalPrice=" + totalPrice + ",orderSource" + orderSource
									+ ",szCarpassId=" + orderId + ",memberId=" + memberId + ",orderId" + orderId));
		}
		ParkDataResult dataResult = ParkDataResult.createObjDate();
		dataResult.putDataOnObj("szOrderId", orderId);
		dataResult.putDataOnObj("bjOrderId", resultCreateorder.getOrderId());
		return dataResult;
	}

	@Override
	public ParkDataResult createmonthPayment_Mon(int parkId, String carNum, String bjuserId) throws BaseException {

		DBObject record = demoBiz.demoData_record_findone(carNum, RecordTypeEnum.MONTHRENT_PAY.getCode());

		if (record==null||record.containsField("exception")) {
			// 如果有异常信息
			DBObject exception = (DBObject) record.get("exception");
			String simpleText = (String) exception.get("simpleText");
			throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_MONTHCARD_NOTPAY.getId(), null,
					ExceptionMessage.createExceptionMessage(
							ServiceCoreException.TypeEnum.EXCEPTION_MONTHCARD_NOTPAY.getDesc(), simpleText,
							"车牌=" + carNum));
		}

		DBObject car = demoBiz.demoData_findCar(carNum);
		Date cardExpiration = (Date) car.get("cardExpiration");
//		String orderId = (String) record.get("orderId");
		String szCarpassId = OrderNumUtil.getOrderNum(1, OrderServiceType.MONTHRENT.getTag());
		szCarpassId = szCarpassId + OrderServiceType.DEMO_ORDER.getTag();
		String parkName = (String) record.get("parkName");
		
		DemoUserMonth month=new DemoUserMonth();
		month.setOrderId(szCarpassId);
		month.setParkId(parkId);
		month.setMonthBuy(1);
		month.setCarNum(carNum);
		month.setParkName(parkName);
		month.setOldCardExpiration(cardExpiration);
		demoBiz.demoData_createMonOrder(bjuserId,month);
		
		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		parkDataResult.putDataOnObj("orderId", szCarpassId);
		parkDataResult.putDataOnObj("parkId", parkId);
		parkDataResult.putDataOnObj("parkName", parkName);
		parkDataResult.putDataOnObj("cardExpiration", cardExpiration == null ? 0 : cardExpiration.getTime() / 1000);
		return parkDataResult;
	}

	@Override
	public ParkDataResult calMonthFee(String orderId, int monthNum) {
//		DBObject record = demoBiz.demoData_record_findone(orderId);
		DemoBiz.DemoUserMonth record=demoBiz.findMonth(orderId);
		String carNum = record.getCarNum();
		DBObject car = demoBiz.demoData_findCar(carNum);
		Date cardExpiration = (Date) car.get("cardExpiration");
		int monthFee = (int) car.get("monthFee");
		ParkDataResult parkDataResult = new ParkDataResult();
		parkDataResult.putDataOnObj("totalFee", monthFee * monthNum);
		if (cardExpiration != null) {
			parkDataResult.putDataOnObj("expirationTime",
					DateUtils.addMonths(cardExpiration, monthNum).getTime() / 1000);
		}
		
		demoBiz.demoData_calMonthFee(orderId,monthNum);
		return parkDataResult;
	}

	@Override
	public ParkDataResult createInvite(TbInvitecard tbInvitecard) {
		// TODO Auto-generated method stub
		return ParkDataResult.createEmpty();
	}

	@Override
	public ParkDataResult createTeneratPayment(int parkId, String carNum, String bjuserId) throws BaseException {
		DBObject record = demoBiz.demoData_record_findone(carNum, demoBiz.TENERAT_PAY_TYPE);
		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		if (record == null) {
			throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_CAR_NOTIN_PARK.getId(), null,
					ExceptionMessage.createExceptionMessage(
							ServiceCoreException.TypeEnum.EXCEPTION_CAR_NOTIN_PARK.getDesc(), "车辆未进场，无法缴费",
							" 车牌=" + carNum));
		} else {
			
//			String orderId = (String) record.get("orderId");
			String szCarpassId = OrderNumUtil.getOrderNum(1, OrderServiceType.TEMP_PAY.getTag());
			szCarpassId = szCarpassId + OrderServiceType.DEMO_ORDER.getTag();
			String parkName = (String) record.get("parkName");
			int deposit = (int) record.get("deposit");
			
			DemoBiz.DemoUserTemp temp=new DemoBiz.DemoUserTemp();
			temp.setOrderId(szCarpassId);
			temp.setParkId(parkId);
			temp.setCarNum(carNum);
			temp.setParkName(parkName);
			demoBiz.demoData_createTempOrder(bjuserId,temp);
			
			parkDataResult.putDataOnObj("orderId", szCarpassId);
			parkDataResult.putDataOnObj("parkId", parkId);
			parkDataResult.putDataOnObj("parkName", parkName);
			parkDataResult.putDataOnObj("startTime", DateUtils.addMinute(new Date(), -58).getTime() / 1000);
			parkDataResult.putDataOnObj("endTime", new Date().getTime() / 1000);
			parkDataResult.putDataOnObj("parkFee", deposit);
		}
		return parkDataResult;
	}

	@Override
	public ParkDataResult createLockCarOrder(TbCarlock tbCarlock) {
		
		DBObject  record=this.demoBiz.demoData_record_findone(tbCarlock.getParkID(),tbCarlock.getCarNum(), demoBiz.CARLOCK_TYPE);
		if (record==null||record.containsField("exception")) {
			// 车辆没有停在此车场
			throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_CAR_NOTIN_PARK.getId(), null,
					ExceptionMessage.createExceptionMessage(
							ServiceCoreException.TypeEnum.EXCEPTION_CAR_NOTIN_PARK.getDesc(), "车辆未进场，不支持锁车",
							"车场=" + tbCarlock.getParkID() + " 车牌=" + tbCarlock.getCarNum()));
		}
		return ParkDataResult.createEmpty();
	}

	@Override
	public ParkDataResult lockedCarList(String userId) {

		List<DBObject> records = demoBiz.demoData_record_findAll(demoBiz.CARLOCK_TYPE);

		List list = new ArrayList();
		if (!ListUtil.isEmpty(records)) {

			for (DBObject lock : records) {

				String orderId = (String) lock.get("orderId");
				String parkName = (String) lock.get("parkName");
				String carNum = (String) lock.get("carNum");
				Date startTime = (Date) lock.get("startTime");
				Date endTime = (Date) lock.get("endTime");
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("orderId", orderId);
				map.put("parkName", parkName);
				map.put("carNum", carNum);
				map.put("startTime", startTime.getTime() / 1000);
				map.put("endTime", endTime.getTime() / 1000);
				list.add(map);
			}

			ParkDataResult data = ParkDataResult.createListDate();
			data.putDataOnList(list);
			return data;
		} else {
			return ParkDataResult.createEmpty();
		}
	}

	@Override
	public ParkDataResult recordList(String userId, int recordType, int jumpPage, int pageLength) throws BaseException {

		List<DBObject> list = new ArrayList<DBObject>();
		if (recordType == TbHistoryRecord.RecordTypeEnum.ALL_PAY.getCode()) {
			list = demoBiz.demoData_hisAll(jumpPage, pageLength);
		} else if (recordType == TbHistoryRecord.RecordTypeEnum.MONTHRENT_PAY.getCode()) {
			list = demoBiz.demoData_his(jumpPage, pageLength, TbHistoryRecord.RecordTypeEnum.MONTHRENT_PAY.getCode());
		} else if (recordType == TbHistoryRecord.RecordTypeEnum.TEMP_PAY.getCode()) {
			list = demoBiz.demoData_his(jumpPage, pageLength, TbHistoryRecord.RecordTypeEnum.TEMP_PAY.getCode(),
					demoBiz.TENERAT_PAY_TYPE);
		} else if (recordType == TbHistoryRecord.RecordTypeEnum.RESERVE_PAY.getCode()) {
			list = demoBiz.demoData_his(jumpPage, pageLength, TbHistoryRecord.RecordTypeEnum.RESERVE_PAY.getCode());
		}
		PageInfo page = new PageInfo(list);
		List dataList = new ArrayList();
		for (DBObject record : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			int recoType = (int) record.get("recordType");
			String orderId = (String) record.get("orderId");
			int payFee = (int) record.get("deposit");
			Date feeTime = (Date) record.get("feeTime");
			if (recoType == TbHistoryRecord.RecordTypeEnum.MONTHRENT_PAY.getCode()) {
				// 月租续费
				map.put("recordType", TbHistoryRecord.RecordTypeEnum.MONTHRENT_PAY.getCode());
				map.put("orderId", orderId);
				map.put("payFee", payFee);
				map.put("status", 0);
				map.put("orderSource", 0);
				map.put("payFeeTime", feeTime.getTime() / 1000);
				dataList.add(map);
			} else if (recoType == TbHistoryRecord.RecordTypeEnum.RESERVE_PAY.getCode()) {
				// 预定
				Date reserveTime = (Date) record.get("reserveTime");
				Date realIncheckTime = (Date) record.get("realIncheckTime");
				map.put("recordType", recoType);
				map.put("orderId", orderId);
				map.put("reserveIncheckTime", reserveTime.getTime() / 1000);
				map.put("status", 1);
				map.put("orderSource", 0);
				map.put("realIncheckTime", realIncheckTime.getTime() / 1000);

				dataList.add(map);
			} else if (recoType == TbHistoryRecord.RecordTypeEnum.TEMP_PAY.getCode()) {
				// 临停缴费
//				map.put("recordType", recoType);
//				map.put("payFee", payFee);
//				map.put("orderId", orderId);
//				map.put("status", 0);
//				map.put("orderSource", 0);
//				map.put("payFeeTime", feeTime.getTime() / 1000);
//				dataList.add(map);
			} else if (recoType == demoBiz.TENERAT_PAY_TYPE) {
				// 临停缴费
//				map.put("recordType", TbHistoryRecord.RecordTypeEnum.TEMP_PAY.getCode());
//				map.put("payFee", payFee);
//				map.put("orderId", orderId);
//				map.put("status", 0);
//				map.put("orderSource", 0);
//				map.put("payFeeTime", feeTime.getTime() / 1000);
//				dataList.add(map);
			}
		}

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("list", dataList);
		data.put("currentPage", page.getPageNum());
		data.put("numPerPage", page.getPageSize());
		data.put("totalCount", page.getTotal());
		data.put("hast", page.isHasPreviousPage());
		data.put("next", page.isHasNextPage());
		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		parkDataResult.putMapOnObj(data);
		return parkDataResult;
	}

	@Override
	public ParkDataResult recordInfo(String orderId, int recordType) throws BaseException {
		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		Map<String, Object> map = new HashMap<String, Object>();

		DBObject record = demoBiz.demoData_record_findone(orderId);
		String parkName = (String) record.get("parkName");
		String carNum = (String) record.get("carNum");
		int parkId = (int) record.get("parkId");
		if (recordType == TbHistoryRecord.RecordTypeEnum.MONTHRENT_PAY.getCode()) {
			// 月租续费
			int monthNum = (int) record.get("monthNum");
			map.put("orderId", orderId);
			map.put("monthNum", monthNum);
			map.put("parkName", parkName);
			map.put("carNum", carNum);
			map.put("payFeeTime",
					DateUtils.parseDate("2017-1-29 19:22:34", DateUtils.DATE_FORMAT_DATETIME).getTime() / 1000);
			map.put("expirationTime",
					DateUtils.parseDate("2017-2-28", DateUtils.DATE_FORMAT_DATEONLY).getTime() / 1000);
			map.put("payFee", 50 * 100L);
			map.put("parkId", parkId);
			map.put("orderSource", 0);
			parkDataResult.putMapOnObj(map);

		} else if (recordType == TbHistoryRecord.RecordTypeEnum.RESERVE_PAY.getCode()) {
			// 预定
			Date reserveTime = (Date) record.get("reserveTime");
			Date realIncheckTime = (Date) record.get("realIncheckTime");
			map.put("orderId", orderId);
			map.put("reserveIncheckTime", reserveTime.getTime() / 1000);
			map.put("parkName", parkName);
			map.put("carNum", carNum);
			map.put("status", 1);
			map.put("reserverContect", "");
			map.put("parkId", parkId);
			map.put("orderSource", 0);
			parkDataResult.putMapOnObj(map);
		} else if (recordType == TbHistoryRecord.RecordTypeEnum.TEMP_PAY.getCode()) {
			// 临停缴费
			map.put("orderId", orderId);
			Date startTime = (Date) record.get("startTime");
			Date endTime = (Date) record.get("endTime");
			int deposit = (int) record.get("deposit");
			map.put("reserveIncheckTime", startTime.getTime() / 1000);
			map.put("parkName", parkName);
			map.put("carNum", carNum);
			map.put("realIncheckTime", startTime.getTime() / 1000);
			map.put("realOutcheckTime", endTime.getTime() / 1000);
			map.put("reserveIncheckTime", startTime.getTime() / 1000);
			map.put("payFee", deposit);
			map.put("parkId", parkId);
			map.put("orderSource", 0);
			parkDataResult.putMapOnObj(map);
		}
		return parkDataResult;

	}

	@Override
	public ParkDataResult passList(String userId, int jumpPage, int pageLength) throws BaseException {
		List<DBObject> list = new ArrayList<DBObject>();
		list = demoBiz.demoData_his(jumpPage, pageLength, demoBiz.PASS_TYPE);
		List dataList = new ArrayList();
		for (DBObject record : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			String orderId = (String) record.get("orderId");
			String carNum = (String) record.get("carNum");
			int parkId = (int) record.get("parkId");
			String parkName = (String) record.get("parkName");
			Date startTime = (Date) record.get("startTime");
			Date endTime = (Date) record.get("endTime");
			map.put("recordId", orderId);
			map.put("carNum", carNum);
			map.put("parkId", parkId);
			map.put("parkName", parkName);
			map.put("incheckTime", startTime.getTime() / 1000);
			map.put("outcheckTime", endTime.getTime() / 1000);
			dataList.add(map);
		}
		PageInfo page = new PageInfo(list);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("list", dataList);
		data.put("currentPage", page.getPageNum());
		data.put("numPerPage", page.getPageSize());
		data.put("totalCount", page.getTotal());
		data.put("hast", page.isHasPreviousPage());
		data.put("next", page.isHasNextPage());
		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		parkDataResult.putMapOnObj(data);
		return parkDataResult;
	}

	@Override
	public ParkDataResult inviteList(String userId, int jumpPage, int pageLength) throws BaseException {
		List<DBObject> list = new ArrayList<DBObject>();
		list = demoBiz.demoData_his(jumpPage, pageLength, demoBiz.INVITE_TYPE);
		List dataList = new ArrayList();
		for (DBObject record : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			String orderId = (String) record.get("orderId");
			String carNum = (String) record.get("carNum");
			int parkId = (int) record.get("parkId");
			String parkName = (String) record.get("parkName");
			Date visitTime = (Date) record.get("visitTime");
			Date realIncheckTime = (Date) record.get("realIncheckTime");
			Date createDate = (Date) record.get("createDate");
			int visitPurpose = (int) record.get("visitPurpose");
			int visitAddress = (int) record.get("visitAddress");
			int status = (int) record.get("status");
			map.put("orderId", orderId);
			map.put("parkId", parkId);
			map.put("parkName", parkName);
			map.put("carNum", carNum);
			map.put("visitIncheckTime", realIncheckTime.getTime() / 1000);
			map.put("visitTime", visitTime.getTime() / 1000);
			map.put("status", status);
			map.put("visitPurpose", visitPurpose);
			map.put("visitAddress", visitAddress);
			map.put("createDate", createDate.getTime() / 1000);
			dataList.add(map);
		}
		PageInfo page = new PageInfo(list);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("list", dataList);
		data.put("currentPage", page.getPageNum());
		data.put("numPerPage", page.getPageSize());
		data.put("totalCount", page.getTotal());
		data.put("hast", page.isHasPreviousPage());
		data.put("next", page.isHasNextPage());
		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		parkDataResult.putMapOnObj(data);
		return parkDataResult;
	}

	@Override
	public ParkDataResult lockedCarList(String userId, int jumpPage, int pageLength) throws BaseException {
		List<DBObject> list = new ArrayList<DBObject>();
		list = demoBiz.demoData_his(jumpPage, pageLength, demoBiz.CARLOCK_TYPE);
		List dataList = new ArrayList();
		for (DBObject record : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			String orderId = (String) record.get("orderId");
			String carNum = (String) record.get("carNum");
			int parkId = (int) record.get("parkId");
			String parkName = (String) record.get("parkName");
			Date startTime = (Date) record.get("startTime");
			Date endTime = (Date) record.get("endTime");
			Date createDate = (Date) record.get("createDate");
			Date unlockDate = (Date) record.get("unlockDate");
			int status = (int) record.get("status");
			map.put("orderId", orderId);
			map.put("parkId", parkId);
			map.put("parkName", parkName);
			map.put("carNum", carNum);
			map.put("startTime", startTime.getTime() / 1000);
			map.put("endTime", endTime.getTime() / 1000);
			map.put("status", status);
			map.put("createDate", createDate.getTime() / 1000);
			if (status == CarLockStatusEnum.CANCEL_STATUS.getStatus()
					|| status == CarLockStatusEnum.OVERTIME_STATUS.getStatus()) {
				map.put("unlockDate", unlockDate.getTime() / 1000);
			}
			dataList.add(map);
		}
		PageInfo page = new PageInfo(list);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("list", dataList);
		data.put("currentPage", page.getPageNum());
		data.put("numPerPage", page.getPageSize());
		data.put("totalCount", page.getTotal());
		data.put("hast", page.isHasPreviousPage());
		data.put("next", page.isHasNextPage());
		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		parkDataResult.putMapOnObj(data);
		return parkDataResult;
	}

	@Override
	public ParkDataResult getRuleByParkId(int parkId) throws BaseException {

		List<DBObject> list = demoBiz.demoData_record_findAll(demoBiz.PARKRULE_TYPE, parkId);

		List<DBObject> parkRule = null;
		if (!ListUtil.isEmpty(list)) {
			parkRule = (List<DBObject>) list.get(0).get("parkRule");
		}

		Map<String, Object> data = new HashMap<String, Object>();

		data.put("list", parkRule);

		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		parkDataResult.putMapOnObj(data);
		return parkDataResult;
	}

	@Override
	public ParkDataResult clearSession(String userId) throws BaseException {
		
		demoBiz.clearSession(userId);
		return null;
	}

	@Override
	public ParkDataResult queryReservesByBjuserId(String userId) throws BaseException {
		List<DBObject>  records=demoBiz.demoData_record_findAll(TbHistoryRecord.RecordTypeEnum.RESERVE_PAY.getCode());
		List list = new ArrayList();
		for (DBObject record : records) {
			Map<String, Object> map = new HashMap<String, Object>();
			String carNum=(String) record.get("carNum");
			String orderId=(String) record.get("orderId");
			int parkId=(int) record.get("parkId");
			String parkName=(String) record.get("parkName");
			Date reserveTime=(Date) record.get("reserveTime");
			int deposit=(int) record.get("deposit");
			map.put("carNum", carNum);
			map.put("reserveId", orderId);
			map.put("parkId",parkId);
			map.put("parkName", parkName);
			map.put("reserveTime", reserveTime.getTime() / 1000);
			map.put("deposit", deposit);
			list.add(map);
		}
		ParkDataResult parkDataResult = ParkDataResult.createListDate();
		parkDataResult.putDataOnList(list);
		return parkDataResult;
	}

	@Override
	public ParkDataResult listall(String tel, String bjuserid, String bjprojectId) {
		List<DBObject> parkList=this.demoBiz.demoData_record_findAll(DemoBiz.PARK_RECORD_TYPE);
		List dataList = new ArrayList();
		if(ListUtil.isEmpty(parkList)){
			return ParkDataResult.createListDate();
		}
		for (DBObject park : parkList) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("parkId", park.get("parkId"));
			map.put("parkName", park.get("parkName"));
			dataList.add(map);
		}
		ParkDataResult parkDataResult = ParkDataResult.createListDate();
		parkDataResult.putDataOnList(dataList);
		return parkDataResult;
	}
}
